//##include('../block/lazyload.js');
//##include('../block/doT.js');
//##include('../block/swiper.js');
//##include('../block/initPhotoSwipeIndex.js');
//##include('../block/dropload.js');


var $doc,$header,$fullMaskLoading,$pedialist;

var _view = {
    init: function() {
        this.closeFullMaskLoading();
        //this.droprefresh();
        this.initSwiperFn();
        this.lazyimg();
    },

    droprefresh: function(){
        // dropload

        setTimeout(function(){
            $('.content').dropload({
                scrollArea : window,
                domUp : {
                    domClass   : 'dropload-up',
                    domRefresh : '<div class="dropload-refresh">↓下拉刷新</div>',
                    domUpdate  : '<div class="dropload-update">↑请松手</div>',
                    domLoad    : '<div class="dropload-load"><span class="loading"></span>加载中...</div>'
                },
                domDown : {
                    domClass   : 'dropload-down',
                    domRefresh : '<div class="dropload-refresh">↑上拉加载更多</div>',
                    domLoad    : '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                    domNoData  : '<div class="dropload-noData">暂无更多内容</div>'
                },
                loadUpFn : function(me){
                    $.ajax({
                        type: 'GET',
                        url: '/pedia/pedia_apis/pedialist',
                        dataType: 'json',
                        success: function(data){
                            var temID = $newlist.attr('temId');
                            var tmpl = $("#"+temID+"-refresh").html();
                            var doTtmpl = doT.template(tmpl);
                            $newlist.html(doTtmpl(data));
                            me.resetload();
                        },
                        error: function(){
                            console.log('Ajax error!');
                            me.resetload();
                        }
                    })
                },
                loadDownFn : function(me){
                    $.ajax({
                        type: 'GET',
                        url: '/pedia/pedia_apis/pedialist',
                        dataType: 'json',
                        success: function(data){

                            var len = data.list.length;

                            if(len > 0){
                                var temID = $newlist.attr('temId');
                                var tmpl = $("#"+temID).html();
                                var doTtmpl = doT.template(tmpl);
                                $newlist.append(doTtmpl(data));
                            }else{
                                me.lock();
                                // 无数据
                                me.noData();
                            }
                            me.resetload();
                        },
                        error: function(){
                            console.log('Ajax error!');
                            me.resetload();
                        }
                    });
                },
                autoLoad: false,
                distance:200
            });
        },500);
    },

    //初始所有swiper
    initSwiperFn: function(){

        //顶部菜单
        var mySwiperHeadMenu = new Swiper($header,{
            watchSlidesProgress : true,
            watchSlidesVisibility : true,
            slidesPerView : 3

            //分页tab需要
            //onTap: function(){
            //    tabsSwiper.slideTo( mySwiperHeadMenu.clickedIndex)
            //}
        });


        //分页tab
        //var tabsSwiper = new Swiper('#tabs-wrapper',{
        //    onSlideChangeStart: function(){
        //        updateNavPosition()
        //    }
        //});
        //
        //function updateNavPosition(){
        //
        //    $header.find('.active-nav').removeClass('active-nav');
        //    var activeNav = $header.find('.swiper-slide').eq(tabsSwiper.activeIndex).addClass('active-nav');
        //    if (!activeNav.hasClass('swiper-slide-visible')) {
        //
        //        if (activeNav.index()>mySwiperHeadMenu.activeIndex) {
        //            var thumbsPerNav = Math.floor(mySwiperHeadMenu.width/activeNav.width())-1;
        //            mySwiperHeadMenu.slideTo(activeNav.index()-thumbsPerNav)
        //        } else {
        //            mySwiperHeadMenu.slideTo(activeNav.index())
        //        }
        //    }
        //}
    },

    //关闭加载动画
    closeFullMaskLoading : function(){
        if($pedialist.find('.item').length > 0){
            $fullMaskLoading.addClass('hide');
        }
    },
    lazyimg:function(){
        $(".lazy").lazyload({
            effect : "fadeIn"
        });
    },
};

var _event = {
    bind: function() {}
};

var _data = {};

function init() {
    _view.init();
    _event.bind();
}

$(document).ready(function(){

    $doc = $(document);
    $header = $('#header');
    $pedialist = $('#pedia-list');
    $fullMaskLoading = $('.full-mask-loading');

    init();
});
